<?php
/**
 * Created by PhpStorm.
 * User: 10838
 * Date: 2017/11/6
 * Time: 17:45
 */
namespace app\index\controller;


//个人中心管理


use app\common\model\ViplevelModel;
use app\index\model\UserModel;
use mg_api\Mg_api;
use wechat\wechatCallbackapiTest;

class Personal  extends Common{
    //积分管理
    public function integral_list(){
        /*$mg_api = new Mg_api();
        $api_data = [
            'key'=>"isTrue",
            'F_Tel'=>'17775525919'

        ];
        $mg_api_tel = $mg_api->api($api_data);
        $mg_api_tel = json_decode($mg_api_tel,true);
        var_dump($mg_api_tel);die;*/
        $user  = $this->user();
        $this->get_wechat();
        //获取积分明细
        $data = $this->integral_details($user['openid']);
        $this->assign('intedral',$data['JData']);
        $this->assign('user',$user);
        return view('integral_list');
    }
    //积分详情
    public function integral_xiangqing($address,$point){
        $data= [
            'key'=>'Get_LinkMan_PointListbyProJect',
            'wx_openid'=>$_SESSION['openid'],
            'address'=>mb_convert_encoding($address,"utf-8", "gbk")
        ];
        $mg_api = new Mg_api();

        $re = $mg_api->api($data);
        $re = json_decode($re,true);
        if(!$re['Status']){
            $this->error('积分详情获取失败');
        }
        //项目积分具体详情
        $this->assign('integral',$re);
        //总积分
        $this->assign('point',$point);
        $this->assign('address',$data['address']);
        return view('integral_xiangqing');
    }

    //个人资料
    public function geren(){
       $user =  UserModel::find($_SESSION['user_id']);
        $tel = explode('/', $user['user_tel']);
        $this->get_wechat();
        $this->assign('tel',$tel);
       $this->assign('user',$user);
        return view('info');
    }
    protected function user(){
        $user = UserModel::find($_SESSION['user_id']);
        //查询总积分key
        if($user['is_pass'] == '1'){
            $mg_api = new Mg_api();
            $data = [
                'key'=>'Get_LinkMan_Point',
                'wx_openid'=>$user->openid
            ];
            $re = $mg_api->api($data);
            $re = json_decode($re,true);
            $user->jifen = $re['JData']['0']['point'];

            if($user->jifen){
                $re = $this->viplevel($user->jifen);
                //分配等级到模板
                $user["vip_title"] = $re['title'];
                $user['vip_id'] = $re['id'];
            }else{

                $user["vip_title"] = "暂无等级";
                $user['vip_id'] = 0;
                $user['jifens'] =$re['JData']['0']['point'];
            }
            return $user;
        }else{
            $this->error('账号未通过审核');
        }
    }
    //按积分获取会员等级
    protected function viplevel($data){
            $viplevel = ViplevelModel ::all();
        foreach($viplevel as $k=>$v){
            if($data >= $v->range_up and $data < $v->range_down){
                return $v;
            }
        }
    }
    public function get_wechat(){
        if(!isset($_SESSION['openid']) || $_SESSION['openid'] == ""){
            $this->error('没有获取到openid');
        }
        if(!isset($_SESSION['access_token']) || $_SESSION['access_token'] == ""){
            $this->error('没有获取到access_token');
        }
        $wechat = new wechatCallbackapiTest();
        $getuser = $wechat->get_user($_SESSION['openid'],$_SESSION['access_token']);
        // $getuser = $wechat->get_user('oqu6Qv0DM2PBtanzEZFjMHSq2iQ0','4_CA8g4bjQkUGiDyCHFcv4BP3Bk-68LR_S8gerfKfYz6FvYQz3bFYl-Jd4XfA7AUiW5sdQNKnAUSfb6J8EN2lHkg');
        //获取微信信息
        $this->assign('wechat_user',$getuser);
    }
    //获取积分明细
    protected function integral_details($openid){
        $mg_api = new Mg_api();
        $data = [
            'key'=>'Get_LinkMan_PointList',
            'wx_openid'=>$openid
        ];
        $re = $mg_api->api($data);
        $re = json_decode($re,true);
        return $re;
    }
}